package cn.ibdsr.human.common.factory;

import cn.ibdsr.human.constant.JwtConstants;
import cn.ibdsr.human.db.mapper.*;

import cn.ibdsr.human.db.model.*;
import cn.ibdsr.human.interceptor.SpringContextHolder;
import cn.ibdsr.human.util.RedisUtil;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 常量的生产工厂
 *
 * @author fengshuonan
 * @date 2017年2月13日 下午10:55:21
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {


    private RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
    private PoliticMapper politicMapper = SpringContextHolder.getBean(PoliticMapper.class);
    private MarryMapper marryMapper = SpringContextHolder.getBean(MarryMapper.class);
    private EducationMapper educationMapper = SpringContextHolder.getBean(EducationMapper.class);
    private ExperienceMapper experienceMapper = SpringContextHolder.getBean(ExperienceMapper.class);

    private CompanyProperMapper companyProperMapper = SpringContextHolder.getBean(CompanyProperMapper.class);
    private WorkerTypeMapper workerTypeMapper = SpringContextHolder.getBean(WorkerTypeMapper.class);


    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }

    @Override
    public String getPolitic(Integer politic,boolean IS_NULL) {
        if(politic==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.POLITIC + ":" + politic;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(politic.equals(0)){
                return JwtConstants.NO_LIMIT;
            }

            Politic politictemp = politicMapper.selectById(politic);
            if (politictemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, politictemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return politictemp.getName();
        }
    }

    @Override
    public String getEducation(Integer education,boolean IS_NULL) {
        if(education==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.EDUCATION + ":" + education;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(education.equals(0)){
                return JwtConstants.NO_LIMIT;
            }
            Education educationTemp = educationMapper.selectById(education);
            if (educationTemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, educationTemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return educationTemp.getName();
        }
    }

    @Override
    public String getMarried(Integer married,boolean IS_NULL) {
        if(married==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.MARRIED + ":" + married;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(married.equals(0)){
                return JwtConstants.NO_LIMIT;
            }
            Marry marryTemp = marryMapper.selectById(married);
            if (marryTemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, marryTemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return marryTemp.getName();
        }
    }

    @Override
    public String getExperience(Integer experience,boolean IS_NULL) {
        if(experience==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.EXPERIENCE + ":" + experience;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(experience.equals(0)){
                return JwtConstants.NO_LIMIT;
            }
            Experience experienceTemp = experienceMapper.selectById(experience);
            if (experienceTemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, experienceTemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return experienceTemp.getName();
        }
    }

    /**
     * 获取企业性质
     * @param proper
     * @return
     */
    @Override
    public String getCompanyProper(Integer proper,boolean IS_NULL) {
        if(proper==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.COMPANY_PROPER + ":" + proper;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(proper.equals(0)){
                return JwtConstants.NO_LIMIT;
            }
            CompanyProper companyProperTemp = companyProperMapper.selectById(proper);
            if (companyProperTemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, companyProperTemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return companyProperTemp.getName();
        }
    }
    /**
     * 获取工作类型
     * @param workerType
     * @return
     */
    @Override
    public String getWorkType(Integer workerType,boolean IS_NULL) {
        if(workerType==null)  return JwtConstants.NO_LIMIT;
        String redisKey = JwtConstants.WORK_TYPE + ":" + workerType;
        Object jwtAuthToken = redisUtil.get(redisKey);
        if (jwtAuthToken != null) {
            return String.valueOf(jwtAuthToken);
        } else {
            if(workerType.equals(0)){
                return JwtConstants.NO_LIMIT;
            }
            WorkerType workerTypeTemp = workerTypeMapper.selectById(workerType);
            if (workerTypeTemp == null) {
                if(!IS_NULL){
                    return JwtConstants.NO_LIMIT;
                }else {
                    return "";
                }
            }
            redisUtil.set(redisKey, workerTypeTemp.getName());
            redisUtil.expire(redisKey, JwtConstants.EXPIRE_TIME[1]);
            return workerTypeTemp.getName();
        }
    }
//
//    /**
//     * 获取字典名称
//     */
//    @Override
//    public String getDictName(Long dictId) {
//        if (ToolUtil.isEmpty(dictId)) {
//            return "";
//        } else {
//            Dict dict = dictMapper.selectById(dictId);
//            if (dict == null) {
//                return "";
//            } else {
//                return dict.getName();
//            }
//        }
//    }
//
//    /**
//     * 获取通知标题
//     */
//    @Override
//    public String getNoticeTitle(Long dictId) {
//        if (ToolUtil.isEmpty(dictId)) {
//            return "";
//        } else {
//            Notice notice = noticeMapper.selectById(dictId);
//            if (notice == null) {
//                return "";
//            } else {
//                return notice.getTitle();
//            }
//        }
//    }

//    /**
//     * 查询字典
//     */
//    @Override
//    public List<Dict> findInDict(Long id) {
//        if (ToolUtil.isEmpty(id)) {
//            return null;
//        } else {
//            EntityWrapper<Dict> wrapper = new EntityWrapper<>();
//            List<Dict> dicts = dictMapper.selectList(wrapper.eq("pid", id));
//            if (dicts == null || dicts.size() == 0) {
//                return null;
//            } else {
//                return dicts;
//            }
//        }
//    }


}
